package com.app.teaware.dialog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BillListBean;
import com.app.teaware.bean.BillListData;
import com.app.teaware.goods.PutUpGoodsActivity;
import com.app.teaware.util.ReceiverUtils;
import com.app.teaware.util.UHFUtil;
import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogBuilder;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogView;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.MySharedPreferences;
import my.TimeUtils;


/**
 * @备注 人员状态编辑对话框
 */
public class DialogBind extends QMUIDialogBuilder {


	BasisActivity mContext;

	@BindView(R.id.recyclerview)
	RecyclerView recyclerview;


	@BindView(R.id.edit_wms)
	EditText edit_wms;

	onPeopleStatusEditListener mListener;
	BroadcastReceiver mResultReceiver;
	BillListBean baseListBean;
	MyAdapterList mAdapter;



	public DialogBind(BasisActivity context, BillListBean listBean, onPeopleStatusEditListener listener) {
		super(context);
		mContext = context;
		mListener = listener;
		baseListBean = listBean;
		setCancelable(true);
		setCanceledOnTouchOutside(false);
	}

	public DialogBind(BasisActivity context) {
		super(context);
		mContext = context;
	}

	public  void setKW(String kw){
		edit_wms.setText(kw);
	}


	private void initData() {
		if(baseListBean == null) return;

		LinearLayoutManager noBindLayoutManager = new LinearLayoutManager(mContext);
		noBindLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		recyclerview.setLayoutManager(noBindLayoutManager);

		mAdapter = new MyAdapterList();
		recyclerview.setAdapter(mAdapter);

		ReceiverUtils.unRegisterReceiver(mContext,mResultReceiver);

	}



	@OnClick(R.id.btn_cancel)
	void onCloseClick(){
		if(mListener != null){
			mListener.onCancel();
		}
		mDialog.dismiss();
	}


	@OnClick(R.id.btn_ok)
	void post() {

		String wms = edit_wms.getText().toString().trim();
		if(TextUtils.isEmpty(wms)){
			mListener.onToast("库位码不可为空");
			return;
		}


		if(mListener != null){
			mListener.onBind(wms);
			mDialog.dismiss();
		}

	}



	@Nullable
	@Override
	protected View onCreateContent(@NonNull QMUIDialog dialog, @NonNull QMUIDialogView parent, @NonNull Context context) {
		View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_bind, null);
		ButterKnife.bind(this, inflate);
		setMaxPercent(1);
		initData();
		return inflate;
	}


	public interface onPeopleStatusEditListener {
		abstract void onBind(String kw);
		abstract void onCancel();
		abstract void onToast(String mes);

	}



	public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

		public MyAdapterList() {

		}


		//创建新View，被LayoutManager所调用
		@Override
		public MyAdapterList.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
			View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dialog_bind_list_item, viewGroup, false);
			return new MyAdapterList.ViewHolder(view);
		}

		//将数据与界面进行绑定的操作
		@Override
		public void onBindViewHolder(MyAdapterList.ViewHolder viewHolder, int position) {
			viewHolder.bind(position);
		}

		//获取数据的数量
		@Override
		public int getItemCount() {
			int count = baseListBean == null ? 0 : baseListBean.getList().size();
			return count;
		}

		//自定义的ViewHolder，持有每个Item的的所有界面元素
		public class ViewHolder extends RecyclerView.ViewHolder {
			@BindView(R.id.text_qrcode)
			public TextView text_qrcode;
			@BindView(R.id.list_item)
			public View list_item;
			@BindView(R.id.img_checkbox)
			public ImageView img_check_box;


			public ViewHolder(View view) {
				super(view);
				ButterKnife.bind(this, view);
			}

			public void bind(int position) {
				BillListData data = baseListBean.getList().get(position);
				text_qrcode.setText("" + data.getRfid());
				if(data.isCheck()){
					img_check_box.setImageResource(R.drawable.checkbox_blue_icon_selected);
				}else {
					img_check_box.setImageResource(R.drawable.checkbox_blue_icon_unselected);
				}
				list_item.setTag(data);

			}

			@OnClick(R.id.list_item)
			public void onItemClick(View v) {
				BillListData data = (BillListData) v.getTag();
				data.setCheck(!data.isCheck());
				notifyItemChanged(getAdapterPosition());
			}


		}
	}



}
